<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class K360receivecustomer extends CI_Model {

    protected $_CI;

    public static $searchField = array(
        'customer_status' => 'customer_status',
        'customer_name' => 'customer_name',
        'customer_time_create_start' => 'customer_time_create >=',
        'customer_time_create_end' => 'customer_time_create <=',
        'customer_phone' => 'customer_phone',
        'gift_type' => '360_receive_customer.gift_type',
        'customer_id' => '360_receive_customer.customer_id',
    );

    public static $likeField = array(
        'customer_address' => 'customer_address',
        'customer_weixin' => 'customer_weixin',
        'customer_company_name' => 'customer_company_name',
        'customer_company_address' => 'customer_company_address',
    );
    
    public static $sortField = array(
        'gift_type' => '360_receive_customer.gift_type',
    );


    public function __construct(){
        parent::__construct();
        $this->_CI = & get_instance();
    }
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
    public function getCustomerListSection($assist){
        if(!empty($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('360_receive_customer');
        if(!empty($assist['where'])) $this->db->where($assist['where']);
        if(!empty($assist['like'])) $this->db->like($assist['like']);
        if(!empty($assist['where_in'])){
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    return array(array(),0);
                }
            }
        }
        if(!empty($assist['pagination'])) {
            $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);
        }

        if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
        $this->db->order_by('360_receive_customer.customer_id','desc');
        if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(empty($assist['pagination'])) {
            $count = count($list);
        }

        return array($list,$count);
    }


    public function getCustomerById($customer_id){
        if(!is_array($customer_id)) $customer_id = array($customer_id);
        if(empty($customer_id)) return array();

        $this->db->from('360_receive_customer');
        $this->db->where_in('360_receive_customer.customer_id',$customer_id);
        $query = $this->db->get();
        $list = $query->result_array();

        return $list;
    }


    public function getCustomer($customer_id){
        $this->db->from('360_receive_customer');
        $this->db->where('360_receive_customer.customer_id',$customer_id);
        $this->db->limit(1);
        $query = $this->db->get();
        $customer = $query->row_array();

        return $customer;
    }


    public function getCustomerByPhone($customer_phone,$select = ''){
        if (!empty($select)) $this->db->select($select);
        $this->db->from('360_receive_customer');
        $this->db->where('customer_phone',$customer_phone);
        $this->db->limit(1);
        $query = $this->db->get();
        $customer = $query->row_array();

        return $customer;
    }

/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
    public function createCustomer($info){
        $db_debug = $this->db->db_debug;
        // $this->db->db_debug = FALSE; 
        $m = $this->db->insert('360_receive_customer',$info);
        $customer_id = $this->db->insert_id();
        $this->db->db_debug = $db_debug;

        return $m ? $customer_id : 0;
    }


/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
    public function editCustomer($info) {
        $this->db->where('customer_id',$info['customer_id']);
        unset($info['customer_id']);
        $this->db->update('360_receive_customer',$info);
    }

/*
| -------------------------------------------------------------------
|  CHECK Functions
| -------------------------------------------------------------------
*/
	public function checkcusomerExisted($customer_phone){
        $this->db->from('360_receive_customer');
        $this->db->where('customer_phone',$customer_phone);
        $count = $this->db->count_all_results();

        return ($count > 0);
    }

}